package com.ledim.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.ledim.adapter.base.LedimBaseListAdapter;
import com.ledim.bean.ContributeListBean;
import com.ledim.bean.RoomAddPlayListResponse;
import com.ledim.bean.RoomInfoResponse;
import com.ledim.service.LedimApiServiceUtils;
import com.ledim.service.LedimHttpObserver;
import com.ledim.utils.FrescoUtil;
import com.ledim.utils.ToastUtil;
import com.ledim.widget.view.LedimExpandableTextview;
import com.letv.android.young.client.R;

/**
 * 我的投稿
 */
public class ContributeAdapter extends LedimBaseListAdapter<ContributeListBean> {

    public ContributeAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        notifyDataSetChanged();
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.contribute_item, null);
            holder.mImage = (SimpleDraweeView) convertView.findViewById(R.id.frescro_image);
            holder.mDuration = (TextView) convertView.findViewById(R.id.tv_duration);
            holder.mTitle = (TextView) convertView.findViewById(R.id.tv_title);
            holder.mDes =
                    (LedimExpandableTextview) convertView.findViewById(R.id.ledim_expandabel_view);
            holder.addPlay = (Button) convertView.findViewById(R.id.bt_add_play);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final ContributeListBean contributeListBean = getItem(position);
        if (contributeListBean != null) {
            holder.mTitle.setText(contributeListBean.title);
            if (!TextUtils.isEmpty(contributeListBean.videoPicStr)) {
                FrescoUtil.loadUrl(contributeListBean.videoPicStr, holder.mImage);
            }
            holder.mDes.setData(contributeListBean.desc, position, contributeListBean.createTime);
            holder.mDuration.setText(contributeListBean.durationStr);

            holder.addPlay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getRoomId(contributeListBean);
                }
            });
        }
        return convertView;
    }

    public static class ViewHolder {

        public SimpleDraweeView mImage;
        public TextView mDuration;
        public TextView mTitle;
        public LedimExpandableTextview mDes;
        public Button addPlay;
    }

    /**
     * 获取roomid
     *
     * @param contributeListBean
     */
    private void getRoomId(final ContributeListBean contributeListBean) {
        LedimApiServiceUtils.getMyRoomInfo(new LedimHttpObserver<RoomInfoResponse>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                ToastUtil.toast("内容太高能,添加失败");
            }

            @Override
            public void onNext(RoomInfoResponse response) {
                super.onNext(response);
                if (response.success) {
                    addPlayList(response.data.roomId, contributeListBean);
                } else {
                    ToastUtil.toast("内容太高能,添加失败");
                }
            }
        });
    }

    /**
     * 添加到播放列表里面
     *
     * @param roomId
     * @param bean
     */
    private void addPlayList(String roomId, ContributeListBean bean) {
        LedimApiServiceUtils.addRoomPlayListByVid(roomId, "0", String.valueOf(bean.vid), bean.title, bean.durationStr, new LedimHttpObserver<RoomAddPlayListResponse>() {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                ToastUtil.toast("内容太高能,添加失败");
            }

            @Override
            public void onNext(RoomAddPlayListResponse response) {
                super.onNext(response);
                if (response != null && response.success) {
                    ToastUtil.toast("好了，大王！");
                } else {
                    ToastUtil.toast("内容太高能,添加失败");
                }
            }
        });
    }

}